/**
 * @FILE:GridfsDAOImpl.java
 * @AUTHOR:chenjian@tmxmall.com
 * @DATE:2014-11-6 上午11:25:01
 **/
package com.it.mongo.model.dto;

import com.mongodb.DB;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * GridFS是一种将大型文件存储在MongoDB的文件规范。所有官方支持的驱动均实现了GridFS规范。
 *
 * 数据库支持以BSON格式保存二进制对象。 但是MongoDB中BSON对象最大不能超过16MB。
 * GridFS 规范提供了一种透明的机制，可以将一个大文件分割成为多个较小的文档。
 * 这将容许我们有效的保存大的文件对象，特别对于那些巨大的文件，比如视频。
 */
@Repository("gridfsDAO")
public class GridfsDAO {
	@Autowired
    MongoTemplate mongoTemplate;

	public long insertFile(InputStream input, String fileName, String bucket) {
		DB db = mongoTemplate.getMongoDbFactory().getLegacyDb();
		GridFS fs = new GridFS(db, bucket);
		GridFSInputFile fsFile = fs.createFile(input);
		fsFile.setId(fileName);
		fsFile.setFilename(fileName);
		fsFile.put("uploadDate", new Date());
//		fsFile.put("contentType",fileName.substring( fileName.lastIndexOf(".") ));
		fsFile.save();
		return 1;
	}

	public InputStream loadFile(String fileName, String bucket) {
		DB db = mongoTemplate.getMongoDbFactory().getLegacyDb();
		GridFS fs = new GridFS(db, bucket);
		List<GridFSDBFile> list = fs.find(fileName);

		if (CollectionUtils.isEmpty(list)){
			return null;
		}

		GridFSDBFile res = list.get(0);

		return res.getInputStream();
	}

	public GridFSDBFile loadGridFile(String fileName, String bucket) {
		DB db = mongoTemplate.getMongoDbFactory().getLegacyDb();
		GridFS fs = new GridFS(db, bucket);
		List<GridFSDBFile> list = fs.find(fileName);

		if (CollectionUtils.isEmpty(list)){
			return null;
		}

		GridFSDBFile res = list.get(0);

		return res;
	}

	public long updateFile(InputStream input, String fileName, String bucket) {
		//先删除
		DB db = mongoTemplate.getMongoDbFactory().getLegacyDb();
		GridFS fs = new GridFS(db, bucket);
		fs.remove(fileName);
		//再更新
		GridFSInputFile fsFile = fs.createFile(input);
		fsFile.setFilename(fileName);
		fsFile.put("uploadDate", new Date());
		fsFile.put("contentType",fileName.substring( fileName.lastIndexOf(".") )); // Bug：后缀格式不对，此时的fileName是用户的userName（email），参照下面updateImageFile
		fsFile.save();

		return 1;
	}

	public long updateImageFile(InputStream input, String fileName, String contentType, String bucket) {
		//先删除
		DB db = mongoTemplate.getMongoDbFactory().getLegacyDb();
		GridFS fs = new GridFS(db, bucket);
		fs.remove(fileName);
		//再更新
		GridFSInputFile fsFile = fs.createFile(input);
		fsFile.setFilename(fileName);
		fsFile.put("uploadDate", new Date());

		String type = "jpg"; // 默认jpg格式
		if(contentType.lastIndexOf("/") > -1){
			type = contentType.substring(contentType.lastIndexOf("/")+1);
		}
		fsFile.put("contentType",type);
		fsFile.save();

		return 1;
	}

	public long deleteFile(String fileName, String bucket) {
		DB db = mongoTemplate.getMongoDbFactory().getLegacyDb();
		GridFS fs = new GridFS(db, bucket);
		fs.remove(fileName);
		return 1;
	}

	public GridFSDBFile loadImageFile(String fileName, String bucket) {
		DB db = mongoTemplate.getMongoDbFactory().getLegacyDb();
		GridFS fs = new GridFS(db, bucket);
		List<GridFSDBFile> list = fs.find(fileName);

		if (CollectionUtils.isEmpty(list)){
			return null;
		}

		GridFSDBFile res = list.get(0);

		return res;
	}
}

